import { createRouter, createWebHistory } from 'vue-router'
import { useMetaStore } from '@/stores'
import { userTokenStore } from '@/stores'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
		{
			path: '/',
			name: 'home',
			component: () => import('@/views'),
			meta: {
				title: '慧就-就业管理平台',
				name: '首页'
			},
			children: [
				{
					path: '/admin',
					name: 'admin',
					component: () => import('@/views/admin'),
					meta: {
						title: '慧就-管理员列表',
						name: '管理员列表'
					}
				},
				{
					path: '/student',
					name: 'student',
					component: () => import('@/views/student'),
					meta: {
						title: '慧就-学生列表',
						name: '学生列表'
					}
				},
				{
					path: '/student/add',
					name: 'studentAdd',
					component: () => import('@/views/student/add'),
					meta: {
						title: '慧就-添加学生',
						name: '添加学生'
					}
				},
				{
					path: '/student/update',
					name: 'studentUpdate',
					component: () => import('@/views/student/update'),
					meta: {
						title: '慧就-修改学生',
						name: '修改学生'
					}
				},
				{
					path: '/student/import',
					name: 'studentImport',
					component: () => import('@/views/student/import'),
					meta: {
						title: '慧就-导入学生',
						name: '批量导入学生信息'
					}
				},
				{
					path: '/center',
					name: 'Center',
					component: () => import('@/views/center'),
					meta: {
						title: '慧就-个人中心',
						name: '个人中心'
					}
				},
				{
					path: '/ai/interview',
					name: 'interview',
					component: () => import('@/views/ai'),
					meta: {
						title: '慧就-Ai模拟面试',
						name: 'Ai模拟面试'
					}
				},
				{
					path: '/ai/interview/records',
					name: 'interviewRecords',
					component: () => import('@/views/ai/records'),
					meta: {
						title: '慧就-Ai面试记录',
						name: 'Ai面试记录'
					}
				},
				{
					path: '/question/import',
					name: 'questionImport',
					component: () => import('@/views/questions/import'),
					meta: {
						title: '慧就-导入题库',
						name: '导入题库'
					}
				},
				{
					path: '/question',
					name: 'question',
					component: () => import('@/views/questions/list'),
					meta: {
						title: '慧就-题库列表',
						name: '题库列表'
					}
				},
				{
					path: '/homework/interview',
					name: 'HomeWorkInterview',
					component: () => import('@/views/homework/interview'),
					meta: {
						title: '慧就-作业-模拟面试',
						name: '模拟面试记录'
					}
				},
				{
					path: '/homework/question',
					name: 'HomeWorkQuestion',
					component: () => import('@/views/homework/questions'),
					meta: {
						title: '慧就-作业-刷题',
						name: '刷题记录'
					}
				}
			]
		},
		{
			path: '/login',
			name: 'login',
			component: () => import('@/views/login'),
			meta: {
				title: '慧就-登录',
				name: '登录'
			}
		},
		{
			path: '/question/help',
			name: 'questionHelp',
			component: () => import('@/views/questions/help'),
			meta: {
				title: '慧就-上传帮助文档',
				name: '帮助文档'
			}
		}
	]
})
const white_url = [
	'/login',
	'/question/help'
]
router.beforeEach((to, from, next) => {
	const tokenStore = userTokenStore()
	for (const i in white_url) {
			if (to.path === white_url[i]) {
				next()
				return
			}
		}
	if (tokenStore.getToken()) {
		const metaStore = useMetaStore()
		const array = []
		for (const i in to.matched) {
			array.push({
				name: to.matched[i].meta.name,
				path: to.matched[i].path
			})
		}
		metaStore.setMeta(array)
		document.title = to.meta.title
		next()
	} else {
		next('/login')
	}
})
export default router
